package net.findbugs.demo.analysis;

import org.apache.bcel.classfile.Method;

import edu.umd.cs.findbugs.ba.CFG;
import edu.umd.cs.findbugs.ba.DepthFirstSearch;
import edu.umd.cs.findbugs.classfile.CheckedAnalysisException;
import edu.umd.cs.findbugs.classfile.IAnalysisCache;
import edu.umd.cs.findbugs.classfile.IMethodAnalysisEngine;
import edu.umd.cs.findbugs.classfile.MethodDescriptor;

public class LockCountDataflowEngine implements IMethodAnalysisEngine<LockCountDataflow> {

	public LockCountDataflow analyze(IAnalysisCache analysisCache,
	        MethodDescriptor descriptor) throws CheckedAnalysisException {
		CFG cfg =
			analysisCache.getMethodAnalysis(CFG.class, descriptor);
		DepthFirstSearch dfs =
			analysisCache.getMethodAnalysis(DepthFirstSearch.class, descriptor);
		Method method =
			analysisCache.getMethodAnalysis(Method.class, descriptor);
		
		LockCountAnalysis analysis = new LockCountAnalysis(dfs, method.isSynchronized());
		
		LockCountDataflow dataflow = new LockCountDataflow(cfg, analysis);
		
		dataflow.execute();
		
		return dataflow;
	}

	public void registerWith(IAnalysisCache analysisCache) {
		analysisCache.registerMethodAnalysisEngine(LockCountDataflow.class, this);
	}

	public boolean canRecompute() {
		return true;
	}

}
